package org.lib.eshello.check;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;

import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientProperties.Sniffer;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * ES检查
 * @author ben
 * @date 2021-09-16 10:24:31 CST
 */
@RestController
@RequestMapping(value="/es")
public class EsController {

	static Consumer<Object> cs = System.out::println;

	@Autowired
	private RestHighLevelClient client;
	
	@Autowired
	private ElasticsearchRestClientProperties properties;
	
	@Autowired
	private ElasticsearchRestTemplate esTemplate;
	
	/**
	 * 检查RestHighLevelClient Bean
	 * @author ben
	 * @date 2021-09-16 10:28:59 CST
	 * @return
	 */
	@GetMapping(value="/check/client")
	public Boolean checkClient() {
		cs.accept("client=" + client);
		RestClient llrc = client.getLowLevelClient();
		if (Objects.nonNull(llrc)) {
			cs.accept("LowLevelClient=" + llrc);
			cs.accept("isRunning=" + llrc.isRunning());
			
			List<Node> list = llrc.getNodes();
			cs.accept("list.size=" + list.size());
			Node node = list.get(0);
			cs.accept("node[0]=" + node);
			cs.accept("n=" + node.getName());
			cs.accept("v=" + node.getVersion());
			cs.accept("h=" + node.getHost());
			cs.accept("rs=" + node.getRoles());
			cs.accept("as=" + node.getAttributes());
		}
		cs.accept("cluster=" + client.cluster());
		cs.accept("indices=" + client.indices());
		cs.accept("ingest=" + client.ingest());
		cs.accept("machineLearning=" + client.machineLearning());
		return true;
	}

	/**
	 * 检查ElasticsearchRestClientProperties Bean
	 * @author ben
	 * @date 2021-09-16 10:28:59 CST
	 * @return
	 */
	@GetMapping(value="/check/client/properties")
	public Boolean checkClientProperties() {
		cs.accept("---properties=" + properties);
		cs.accept("uris=" + properties.getUris());
		cs.accept("username=" + properties.getUsername());
		cs.accept("password=" + properties.getPassword());
		cs.accept("connectionTimeout=" + properties.getConnectionTimeout().getSeconds());
		cs.accept("readTimeout=" + properties.getReadTimeout().getSeconds());
		
		Sniffer sn = properties.getSniffer();
		if (Objects.nonNull(sn)) {
			cs.accept("---Sniffer=" + sn);
			cs.accept("DelayAfterFailure=" + sn.getDelayAfterFailure().getSeconds());
			cs.accept("Interval=" + sn.getInterval().getSeconds());
		} else {
			cs.accept("Sniffer is null");
		}
		return true;
	}

	/**
	 * 检查ElasticsearchRestTemplate
	 * @author ben
	 * @date 2021-09-16 11:33:58 CST
	 * @return
	 */
	@GetMapping(value="/check/template")
	public Boolean checkEsTemplate() {
		cs.accept("esTemplate=" + esTemplate);
		cs.accept("ElasticsearchConverter=" + esTemplate.getElasticsearchConverter());
		cs.accept("RefreshPolicy=" + esTemplate.getRefreshPolicy());
		cs.accept("RequestFactory=" + esTemplate.getRequestFactory());
		
		return true;
	}
	
}

